<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DaoPessoa
 *
 * @author mariva
 */

abstract class DaoPessoa implements IDao{
    
    
    
    function deletar($id){
        
        $sql = "DELETE FROM TB_PESSOA WHERE PES_in_id = ".$id;
        try{
            mysql_query($sql);
            return true;
        }catch(Exception $e){
            return $e->getMessage();
        }
        
    }
    
    public function editar($obj){
        //$sql = "UPDATE ";
    }
    public function listar($params){
    
        
        
        $sql = "SELECT 
                PES_in_id as idPessoa,
                PES_st_nome as nome,
                PES_st_sobrenome as sobrenome,
                PES_dt_nascimento as nascimento,
                CON_PES_in_id as idContato,
                CON_st_email as email
                FROM db_geraquiz.tb_pessoa
                JOIN db_geraquiz.TB_CONTATO ON CON_PES_in_id = CON_in_id
                ".  createWhere($params);
        try{
            $result = mysql_query($sql);
            return $result;
        } catch (Exception $ex) {
                return $ex->getMessage();
        }
    }
    public function salvar($obj){
        
        $sql = "INSERT INTO TB_PESSOA
                (PES_st_nome,
                PES_st_sobrenome,
                PES_dt_nascimento,
                CON_PES_in_id) VALUES
                (".$obj->getNome().",
                ".$obj->getSobrenome().",
                ".$obj->getNascimento().",
               ".$obj->getIdContato()." )";
        try{
            mysql_query($sql);
            return true;
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
        
    }
}
function createWhere($params){
    $whereSql = "WHERE ";
    foreach ($params as $key=>$value){
           
           
           if(empty($value)){
            switch ($key){
                case 'nomeSearch':
                    
                    $whereSql= $whereSql." PES_st_nome LIKE %".$params['nomeSearch']."% ";
                    if(strlen($whereSql)>6){
               
                        $whereSql = $whereSql." AND "; 
                    }
                case 'sobrenomeSearch':
                    if(strlen($whereSql)>6){
               
                        $whereSql = $whereSql." AND "; 
                    }
                    $whereSql= $whereSql." PES_st_sobrenome LIKE %".$params['sobrenomeSearch']."% ";
                case 'emailSearch':
                    if(strlen($whereSql)>6){
               
                        $whereSql = $whereSql." AND "; 
                    }
                    $whereSql= $whereSql." CON_st_email LIKE %".$params['emailSearch']."% ";
                default :"";
            }
            
           }
        }
        if(strlen($whereSql)>6){
            return $whereSql;
        }else{
            return "";
        }
        
       
}

